#pragma once

#include <iostream>
#include <stdint.h>
#include "except.hh"
#include "util.hh"

namespace WordMemory
{
    class Config
    {
    public:
        static Config &instance()
        {
            static Config cfg;
            return cfg;
        }

        uint16_t           svr_port()   { return _svr_port; }
        const std::string &sql_user()   { return _sql_user; }
        const std::string &sql_passwd() { return _sql_passwd; }
        const std::string &sql_host()   { return _sql_host; }
        uint16_t           sql_port()   { return _sql_port; }
        const std::string &sql_db()     { return _sql_db;   }

    private:
        Config()
        {
            Json::Value js =
                JsonUtil::deserialize(FileUtil::read("resources/config.json"));

            _svr_port = js["svr_port"].asUInt();
            _sql_user = js["sql_user"].asString();
            _sql_passwd = js["sql_passwd"].asString();
            _sql_host = js["sql_host"].asString();
            _sql_port = js["sql_port"].asUInt();
            _sql_db = js["sql_db"].asString();

            spdlog::info("Configuration loaded");
        }

        Config(const Config &) = delete;
        Config &operator=(const Config &) = delete;

    private:
        uint16_t _svr_port;
        std::string _sql_user;
        std::string _sql_passwd;
        std::string _sql_host;
        uint16_t _sql_port;
        std::string _sql_db;
    };
}
